﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shop.Base.Entity;
using Shop.Base.Entity.Delete;
using Shop.Base.Entity.Insert;
using Shop.Base.Entity.Update;
using Shop.Base.Interface;
using Shop.Entity;
using Shop.Server;
using Shop.Tools;

namespace Shop.Base.Server
{
    public class SystemUser : BaseServer<SystemUserPo>, ISystemUser
    {
        public bool Update(UpdateSystemUser user, ComUser com)
        {
            user.UpdateAt=DateTime.Now;
            user.UpdateId = com.Id;
            user.UpdateName = com.UserName;
            var alreadyList = CheckAccount(user.Account);
            if (alreadyList.Any(x => x.Account == user.Account&&x.Id!=user.Id)) throw new Exception($"#登录账号【{user.Account}】已存在");
            return db.Updateable(user).ExecuteCommand() > 0;
        }

        public bool Insert(AddSystemUser user, ComUser com)
        {
            user.CreateId = com.Id;
            user.CreateName = com.UserName;
            var alreadyList = CheckAccount(user.Account);
            if(alreadyList.Any(x=>x.Account==user.Account))throw  new Exception($"#登录账号【{user.Account}】已存在");
            return db.Insertable(user).ExecuteCommand() > 0;
        }

        public List<SystemUserPo> GetPage(string name, string englishName, string account, int isLogin, int isNormal, int isDel, int pageIndex, int pageSize, out int total)
        {
            total = 0;
            return db.Queryable<SystemUserPo>().WhereIF(!name.IsNullOrEmpty(), $" name like @name ",new {name=$"%{name}%"})
                .WhereIF(!englishName.IsNullOrEmpty(), $" englishName like @englishName ", new { englishName = $"%{englishName}%" })
                .WhereIF(!account.IsNullOrEmpty(), $" account like @account ", new { account = $"%{account}%" })
                .WhereIF(isLogin>-1, $" isLogin = @isLogin ", new { isLogin = (isLogin == 0) })
                .WhereIF(isNormal > -1, $" isNormal = @isNormal ", new { isNormal = (isNormal == 1) })
                .WhereIF(isDel > -1, $" isDel = @isDel ", new { isDel = (isDel == 1) })
                .ToPageList(pageIndex,pageSize,ref total);
        }

        public SystemUserPo Login(UserLogin login)
        {
            return db.Queryable<SystemUserPo>().Where(x => x.Account == login.Account && x.IsDel == false).First();
        }

        public bool InitPasswd(Guid userId, string pwd, ComUser com)
        {
            var mo = db.Queryable<SystemUserPo>().Where(y => y.Id == userId).First();
            if (mo == null || mo.Id.IsEmptyGuid()) return false;
            mo.Passwd = pwd;
            mo.UpdateAt = DateTime.Now;
            mo.UpdateId = com.Id;
            mo.UpdateName = com.UserName;
            return db.Updateable(mo).ExecuteCommand()>0;
        }

        public bool ChangePassword(SystemUserPo user)
        {
            var alreadyList = CheckAccount(user.Account);
            if (alreadyList.Any(x => x.Account == user.Account && x.Id != user.Id)) throw new Exception($"#登录账号【{user.Account}】已存在");
            return db.Updateable(user).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 检查账号
        /// </summary>
        /// <param name="account">账号</param>
        private List<SystemUserPo> CheckAccount(string account)
        {
            return db.Queryable<SystemUserPo>().Where(x => x.Account == account).ToList();
        }
    }
}